package com.wjk.kylin.modules.system.dept.controller;

import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.ResponseData;
import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.modules.system.dept.entity.Dept;
import com.wjk.kylin.modules.system.dept.service.IDeptService;
import com.wjk.kylin.modules.system.role.service.IRoleDeptService;
import com.wjk.kylin.modules.system.vo.TreeSelectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020/5/22
 */
@Api("部门")
@RestController
@RequestMapping("/sys/dept")
public class DeptController extends BaseController {

    @Autowired
    private IDeptService deptService;
    @Autowired
    private IRoleDeptService roleDeptService;

    @PreAuthorize("hasAuthority('system:dept:list')")
    @ApiOperation("树形列表")
    @Log(title = "部门-树形列表",businessType =LIST,isSaveResponseData = false)
    @GetMapping("/treeList")
    public ResponseData treeList() {
        List<Dept> treeList = deptService.treeList();
        return ResponseData.success().data("treeList",treeList);
    }

    @PreAuthorize("hasAuthority('system:dept:query')")
    @ApiOperation("详情")
    @Log(title = "部门-详情",businessType = GET)
    @GetMapping("/{deptId}")
    public ResponseData get(@PathVariable String deptId) {
        Dept dept = deptService.getById(deptId);
        return ResponseData.success().data("dept", dept);
    }

    @PreAuthorize("hasAuthority('system:dept:add')")
    @ApiOperation("新增")
    @Log(title = "部门-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody Dept dept) {
        boolean flag = deptService.save(dept);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:dept:edit')")
    @ApiOperation("编辑")
    @Log(title = "部门-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody Dept dept) {
        boolean flag = deptService.update(dept);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:dept:delete')")
    @ApiOperation("删除")
    @Log(title = "部门-删除",businessType = DELETE)
    @DeleteMapping("/{deptId}")
    public ResponseData delete(@PathVariable String deptId) {
        if (deptService.hasChildByDeptId(deptId)) {
            return ResponseData.error("存在子部门,不允许删除");
        }

        boolean flag = deptService.deleteById(deptId);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }

    @ApiOperation("部门下拉树列表")
    @GetMapping("/treeSelect")
    public ResponseData treeSelect() {
        List<Dept> deptList = deptService.treeList();
        List<TreeSelectVO> treeList = deptList.stream().map(TreeSelectVO::new).collect(Collectors.toList());

        return ResponseData.success().data("treeSelect",treeList);
    }

    @ApiOperation("加载对应角色部门列表树")
    @GetMapping(value = "/roleDeptTreeSelect/{roleId}")
    public ResponseData roleDeptTreeSelect(@PathVariable("roleId") String roleId) {
        //查询所有部门 组装树列表
        List<Dept> deptList = deptService.treeList();
        List<TreeSelectVO> treeList = deptList.stream().map(TreeSelectVO::new).collect(Collectors.toList());

        //角色拥有的部门id
        List<String> checkedKeys = roleDeptService.findDeptListByRoleId(roleId);

        return ResponseData.success().data("treeSelect",treeList).data("checkedKeys",checkedKeys);
    }

}